package com.xz.xzutils.module.xzmybatislogplus.actions;

import com.sun.tools.attach.VirtualMachine;
import com.xz.xzutils.utils.MyBatisDetector;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025/3/19-03-19 20:21
 */
public class ReRun {
    public static void attachAgent(String pid, String agentPath) {
        try {
            if (Boolean.TRUE.equals(MyBatisDetector.isMyBatisUsed(null))) {
                System.err.println("项目不存在任何 mybatis 相关依赖。无法启用");
                return;
            }
            MyBatisDetector.isMyBatisUsed(null);
            VirtualMachine attach = VirtualMachine.attach(pid);
            attach.loadAgent(agentPath);
            attach.detach();
        } catch (Throwable t) {
            System.err.println("attach error:\t" + t.getMessage());
        }
    }
}
